package com.zzdx.deployment.web.controller.project;

import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.utils.ExcelUtil;
import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.system.domain.Project;
import com.zzdx.deployment.system.domain.ProjectVersion;
import com.zzdx.deployment.system.domain.SysUser;
import com.zzdx.deployment.system.service.IProjectService;
import com.zzdx.deployment.system.service.IProjectVersionService;
import com.zzdx.deployment.web.controller.common.CommonFunction;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目版本 信息操作处理
 * 
 * @author mason
 * @date 2018-12-27
 */
@Controller
@RequestMapping("/project/projectVersion")
public class ProjectVersionController extends BaseController
{
    private String prefix = "project/projectVersion";
	
	@Autowired
	private IProjectVersionService projectVersionService;
	@Autowired
	private IProjectService projectService;
	
	@RequiresPermissions("project:projectVersion:view")
	@GetMapping()
	public String projectVersion()
	{
	    return prefix + "/projectVersion";
	}
	
	/**
	 * 查询项目版本列表
	 */
	@RequiresPermissions("project:projectVersion:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectVersion projectVersion)
	{
		Long userId = ShiroUtils.getUserId();
		String loginName = ShiroUtils.getLoginName();
		String projectIds = getProjectIdsStr(userId, loginName);

		startPage();
        List<ProjectVersion> list = projectVersionService.selectProjectVersionListWithUserPerm(projectVersion,projectIds);
		return getDataTable(list);
	}

	/**
	 *  获取用户有权限的项目ID，并以","分隔
	 * @param userId 用户ID
	 * @param loginName 用户登录名
	 * @return
	 */
	private String getProjectIdsStr(Long userId, String loginName) {
		//管理员可查询全部项目，其它只能查看自己创建的项目
		List<Project> projectObjects;
		if(SysUser.isAdmin(userId)){
			projectObjects = projectService.selectProjectAll();
		}else{
			Project loginNameProject = new Project();
			loginNameProject.setCreateBy(loginName);
			projectObjects = projectService.selectProjectList(loginNameProject);
		}
		String result = CommonFunction.convertToProjectIdStr(projectObjects);
		return result;
	}


	/**
	 * 导出项目版本列表
	 */
	@RequiresPermissions("project:projectVersion:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectVersion projectVersion)
    {
    	List<ProjectVersion> list = projectVersionService.selectProjectVersionList(projectVersion);
        ExcelUtil<ProjectVersion> util = new ExcelUtil<ProjectVersion>(ProjectVersion.class);
        return util.exportExcel(list, "projectVersion");
    }
	
	/**
	 * 新增项目版本
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		getProjectsByUserId(mmap);
		return prefix + "/add";
	}
	
	/**
	 * 新增保存项目版本
	 */
	@RequiresPermissions("project:projectVersion:add")
	@Log(title = "项目版本", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectVersion projectVersion)
	{
		String loginName = ShiroUtils.getLoginName();
		projectVersion.setCreateBy(loginName);
		projectVersion.setCreateTime(new Date());
		return toAjax(projectVersionService.insertProjectVersion(projectVersion));
	}

	/**
	 * 修改项目版本
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		ProjectVersion projectVersion = projectVersionService.selectProjectVersionById(id);
		mmap.put("projectVersion", projectVersion);
		getProjectsByUserId(mmap);
	    return prefix + "/edit";
	}

	private void getProjectsByUserId(ModelMap mmap) {
		Long userId = ShiroUtils.getUserId();
		String loginName = ShiroUtils.getLoginName();
		//管理员可查询全部项目，其它只能查看自己创建的项目
		if(SysUser.isAdmin(userId)){
			mmap.put("projects",projectService.selectProjectAll());
		}else{
			Project loginNameProject = new Project();
			loginNameProject.setCreateBy(loginName);
			List<Project> projects = projectService.selectProjectList(loginNameProject);
			mmap.put("projects",projects);
		}
	}

	/**
	 * 修改保存项目版本
	 */
	@RequiresPermissions("project:projectVersion:edit")
	@Log(title = "项目版本", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectVersion projectVersion)
	{
		String loginName = ShiroUtils.getLoginName();
		projectVersion.setUpdateBy(loginName);
		projectVersion.setUpdateTime(new Date());
		return toAjax(projectVersionService.updateProjectVersion(projectVersion));
	}
	
	/**
	 * 删除项目版本
	 */
	@RequiresPermissions("project:projectVersion:remove")
	@Log(title = "项目版本", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(projectVersionService.deleteProjectVersionByIds(ids));
	}


	/**
	 * 加载项目版本列表树数据
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData()
	{
		Long userId = ShiroUtils.getUserId();
		String loginName = ShiroUtils.getLoginName();
		String projectIds = getProjectIdsStr(userId, loginName);
		List<Map<String, Object>> tree = projectVersionService.selectProjectVersionTree(projectIds);
		return tree;
	}
}
